<?php 
/*
Template name: Comment_list
*/
get_header();
?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

	<ul id="posts">
	 	<li class="post">
	 		<div class="post_content page commentpage" id="post-<?php the_ID(); ?>">
	 			<h2 class="title"><?php the_title(); ?></h2>
	 			<div class="txt">
	 				<?php the_content(); ?>
	 				<div id="page_commentbox">
		<?php
		global $wpdb;
		$num_per_page = 20;
		$pages_to_show = 3;
		
		if( isset($_GET['page']) ) {
			$page_now = intval($_GET['page']);
		} 
		else {
			$page_now = 1;
		}
		$comnum = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts AS p, $wpdb->comments AS c WHERE p.ID=c.comment_post_ID AND p.post_status='publish' AND c.comment_approved='1' AND c.comment_type=''");
		$max_page = ceil($comnum/$num_per_page);
		if( $page_now<1 || $max_page<$page_now) {
			$page_now = 1;
		}
		$offset = ($page_now-1) * $num_per_page;
		$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,comment_author_email,comment_date,    comment_post_ID, comment_author, comment_date_gmt, comment_approved,    comment_type,comment_author_url,    SUBSTRING(comment_content,1,52) AS com_excerpt    FROM $wpdb->comments    LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =    $wpdb->posts.ID)    WHERE comment_approved = '1' AND comment_type = '' AND    post_password = ''    ORDER BY comment_date_gmt DESC LIMIT $offset, $num_per_page";
		$comments = $wpdb->get_results($sql);
		$out_page = '<div id="pagenav"><div class="wp-pagenavi"><span class="pages">共有 '.$comnum.' 篇评论</span>';
		if( $max_page>1 ) {
			if( $page_now>1 ) {
				$out_page .= '<a href="'.$linkurl.'?page='.($page_now-1).'">上一页</a> ';
				}
			if( $page_now>($pages_to_show+1) ) {
				$out_page .= '<a href="'.$linkurl.'?page=1">1</a> ';
			}
			if( $page_now>($pages_to_show+2) ) {
				$out_page .= ' ... ';
			}
			for( $i=$page_now-$pages_to_show ; $i<=$page_now+$pages_to_show; $i++ ) {
				if( $i>=1 && $i<=$max_page ) {
					if($i == $page_now) {
						$out_page .= '<span class="current">'.$i.'</span>';
					} 
					else {
						$out_page .= ' <a href="'.$linkurl.'?page='.$i.'">'.$i.'</a> ';
					}
				}
			}
			if( $page_now+$pages_to_show+1 < $max_page ) {
				$out_page .= ' ... ';
			}
			if( $page_now+$pages_to_show < $max_page ) {
				$out_page .= ' <a href="'.$linkurl.'?   page='.$max_page.'">'.$max_page.'</a> ';
			}
			if( $page_now<$max_page ) {
				$out_page .= '<a href="'.$linkurl.'?page='.($page_now+1).'">下一页</a>';
			}
		}
		
		$out_page .= '</div></div>';
		echo "<ol class='commentlist'>";
		foreach ($comments as $comment) {//输出
			$output .= $comment->comment_ID%2==0?"<li id=comment-".$comment->comment_ID.">":"<li id=comment-".$comment->comment_ID.">";
			$output .= "<div class='comment-author vcard'>".get_avatar( $comment->comment_author_email, 32 );
			$output .= "<div class='comment-meta commentmetadata'>".$comment->comment_author."</div>";
			$output .= "<span class='fn'>".$comment->comment_date."</span></div>";
			$output .= "<p class='clean'><a href=".get_permalink($comment->ID)."#comment-" . $comment->comment_ID . " title='Permalink to comment' style='font-weight:bold;'>".$comment->post_title."</a><br />".strip_tags($comment->com_excerpt)."</p>";
			$output .= "</li>";
		}
		echo $output;
		echo "</ol>";
		echo $out_page;
		?>
	</div>
	 			</div>
	 			
	 		</div>
	 	</li>
	</ul>


<?php
	endwhile;
	else :
		echo walleve_404_message();
	endif;
?>


<?php get_footer(); ?>